//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FwdPeriodAdapter.h"
using namespace Cephei::QL::Models::Marketmodels::Models;
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#include <gen/QL/Models/Marketmodels/MarketModel.h>
#include <gen/QL/Math/Matrix.h>
using namespace Cephei::QL::Models::Marketmodels;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Models::CFwdPeriodAdapter::CFwdPeriodAdapter (Cephei::QL::Models::Marketmodels::IMarketModel^ largeModel, UInt64 period, UInt64 offset, Cephei::Core::IVector<Double>^ newDisplacements_) : CMarketModel(CFwdPeriodAdapter::typeid)
{
    CMarketModel^ _ClargeModel;
    try
    {
#ifdef HANDLE
        _phFwdPeriodAdapter = NULL;
#endif
        _ClargeModel = safe_cast<CMarketModel^> (largeModel);
        _ClargeModel->Lock();
        boost::shared_ptr<QuantLib::MarketModel>& _largeModel = static_cast<boost::shared_ptr<QuantLib::MarketModel>&> (_ClargeModel->GetShared ()); 
        QuantLib::Size _period = (QuantLib::Size)ValueHelper::Convert (period); //d
        QuantLib::Size _offset = (QuantLib::Size)ValueHelper::Convert (offset); //d
        newDisplacements_->Lock();
        INativeVector<Double>^ _NCInewDisplacements_ = newDisplacements_->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCnewDisplacements_ = safe_cast<CDoubleVector^>(_NCInewDisplacements_);
        std::vector<QuantLib::Spread>& _newDisplacements_ = static_cast<std::vector<QuantLib::Spread>&> (_NCnewDisplacements_->GetReference ());
        _ppFwdPeriodAdapter = new boost::shared_ptr<QuantLib::FwdPeriodAdapter> (new QuantLib::FwdPeriodAdapter ( _largeModel,  _period,  _offset,  _newDisplacements_ ));
        SetMarketModel (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppFwdPeriodAdapter));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_ClargeModel != nullptr) _ClargeModel->Unlock();
        if (newDisplacements_ != nullptr) newDisplacements_->Unlock();    //not optional
    }
}
Cephei::QL::Models::Marketmodels::Models::CFwdPeriodAdapter::CFwdPeriodAdapter (boost::shared_ptr<QuantLib::FwdPeriodAdapter>& childNative, Object^ owner) : CMarketModel(CFwdPeriodAdapter::typeid)
{
#ifdef HANDLE
	_phFwdPeriodAdapter = NULL;
#endif
	_ppFwdPeriodAdapter = &childNative;
    _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppFwdPeriodAdapter));
}
Cephei::QL::Models::Marketmodels::Models::CFwdPeriodAdapter::CFwdPeriodAdapter (QuantLib::FwdPeriodAdapter& childNative, Object^ owner) : CMarketModel(CFwdPeriodAdapter::typeid)
{
#ifdef HANDLE
	_phFwdPeriodAdapter = NULL;
#endif
	_ppFwdPeriodAdapter = new boost::shared_ptr<QuantLib::FwdPeriodAdapter> (&childNative);
    _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppFwdPeriodAdapter));
    _FwdPeriodAdapterOwner = owner;
    _MarketModelOwner = owner;
}

Cephei::QL::Models::Marketmodels::Models::CFwdPeriodAdapter::CFwdPeriodAdapter (CFwdPeriodAdapter^ copy) : CMarketModel(CFwdPeriodAdapter::typeid)
{
#ifdef HANDLE
	_phFwdPeriodAdapter = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFwdPeriodAdapter = new boost::shared_ptr<QuantLib::FwdPeriodAdapter> (copy->GetShared());
        _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppFwdPeriodAdapter));
    }
}
Cephei::QL::Models::Marketmodels::Models::CFwdPeriodAdapter::CFwdPeriodAdapter (PLATFORM::Type^ t) : CMarketModel(CFwdPeriodAdapter::typeid)
{
#ifdef HANDLE
	_phFwdPeriodAdapter = NULL;
#endif
	if (!t->IsSubclassOf(CFwdPeriodAdapter::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Models::CFwdPeriodAdapter::CFwdPeriodAdapter (QuantLib::Handle<QuantLib::FwdPeriodAdapter>& childNative, Object^ owner)  : CMarketModel(CFwdPeriodAdapter::typeid)
{
	_phFwdPeriodAdapter = &childNative;
	_ppFwdPeriodAdapter = &static_cast<boost::shared_ptr<QuantLib::FwdPeriodAdapter>>(childNative.currentLink());
    _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppFwdPeriodAdapter));
    _FwdPeriodAdapterOwner = owner;
}
Cephei::QL::Models::Marketmodels::Models::CFwdPeriodAdapter::CFwdPeriodAdapter (QuantLib::Handle<QuantLib::FwdPeriodAdapter> childNative)  : CMarketModel(CFwdPeriodAdapter::typeid)
{
	_phFwdPeriodAdapter = &childNative;
	_ppFwdPeriodAdapter = &static_cast<boost::shared_ptr<QuantLib::FwdPeriodAdapter>>(childNative.currentLink());
    _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppFwdPeriodAdapter));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Models::CFwdPeriodAdapter::CFwdPeriodAdapter (QuantLib::FwdPeriodAdapter childNative)  : CMarketModel(CFwdPeriodAdapter::typeid)
{
#ifdef HANDLE
	_phFwdPeriodAdapter = NULL;
#endif
	_ppFwdPeriodAdapter = new boost::shared_ptr<QuantLib::FwdPeriodAdapter> (new QuantLib::FwdPeriodAdapter (childNative));
    _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppFwdPeriodAdapter));
}
#endif

Cephei::QL::Models::Marketmodels::Models::CFwdPeriodAdapter::~CFwdPeriodAdapter ()
{
    if (_ppFwdPeriodAdapter != NULL)
    {
	    delete _ppFwdPeriodAdapter;
        _ppFwdPeriodAdapter = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Models::CFwdPeriodAdapter::!CFwdPeriodAdapter ()
{
    if (_ppFwdPeriodAdapter != NULL)
    {
	    delete _ppFwdPeriodAdapter;
    }
}
QuantLib::FwdPeriodAdapter& Cephei::QL::Models::Marketmodels::Models::CFwdPeriodAdapter::GetReference ()
{
    if (_ppFwdPeriodAdapter == NULL) throw REFNEW NativeNullException ();
	return **_ppFwdPeriodAdapter;
}
boost::shared_ptr<QuantLib::FwdPeriodAdapter>& Cephei::QL::Models::Marketmodels::Models::CFwdPeriodAdapter::GetShared ()
{
    if (_ppFwdPeriodAdapter == NULL) throw REFNEW NativeNullException ();
	return *_ppFwdPeriodAdapter;
}
QuantLib::FwdPeriodAdapter* Cephei::QL::Models::Marketmodels::Models::CFwdPeriodAdapter::GetPointer ()
{
    if (_ppFwdPeriodAdapter == NULL) throw REFNEW NativeNullException ();
	return &**_ppFwdPeriodAdapter;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FwdPeriodAdapter>& Cephei::QL::Models::Marketmodels::Models::CFwdPeriodAdapter::GetHandle ()
{
	if (_phFwdPeriodAdapter == NULL)
	{
		_phFwdPeriodAdapter = new Handle<QuantLib::FwdPeriodAdapter> (*_ppFwdPeriodAdapter);
	}
	return *_phFwdPeriodAdapter;
}
#endif
bool Cephei::QL::Models::Marketmodels::Models::CFwdPeriodAdapter::HasNative () 
{
	return (_ppFwdPeriodAdapter != NULL);
}

Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Models::CFwdPeriodAdapter::Displacements::get ()
{
    try
    {
    	std::vector<QuantLib::Spread>& _rv = (std::vector<QuantLib::Spread>&)(*_ppFwdPeriodAdapter)->displacements ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::IEvolutionDescription^ Cephei::QL::Models::Marketmodels::Models::CFwdPeriodAdapter::Evolution::get ()
{
    try
    {
    	QuantLib::EvolutionDescription& _rv = (QuantLib::EvolutionDescription&)(*_ppFwdPeriodAdapter)->evolution ( );   
        Cephei::QL::Models::Marketmodels::CEvolutionDescription^ _nrv = REFNEW Cephei::QL::Models::Marketmodels::CEvolutionDescription (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Models::CFwdPeriodAdapter::InitialRates::get ()
{
    try
    {
    	std::vector<QuantLib::Rate>& _rv = (std::vector<QuantLib::Rate>&)(*_ppFwdPeriodAdapter)->initialRates ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Models::CFwdPeriodAdapter::NumberOfFactors::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppFwdPeriodAdapter)->numberOfFactors ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Models::CFwdPeriodAdapter::NumberOfRates::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppFwdPeriodAdapter)->numberOfRates ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Models::CFwdPeriodAdapter::NumberOfSteps::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppFwdPeriodAdapter)->numberOfSteps ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Models::Marketmodels::Models::CFwdPeriodAdapter::PseudoRoot (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
    	QuantLib::Matrix& _rv = (QuantLib::Matrix&)(*_ppFwdPeriodAdapter)->pseudoRoot ( _i );   
        Cephei::QL::Math::CMatrix^ _nrv = REFNEW Cephei::QL::Math::CMatrix (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Models::IFwdPeriodAdapter^ Cephei::QL::Models::Marketmodels::Models::CFwdPeriodAdapter_Factory::Create (Cephei::QL::Models::Marketmodels::IMarketModel^ largeModel, UInt64 period, UInt64 offset, Cephei::Core::IVector<Double>^ newDisplacements_)
{
    return REFNEW CFwdPeriodAdapter ( largeModel,  period,  offset,  newDisplacements_);
}
